package com.iwuyc.tools.commons.codec.type;

import com.iwuyc.tools.commons.codec.Codec;
import com.iwuyc.tools.commons.codec.TypeEnum;
import com.iwuyc.tools.commons.util.collection.ArrayUtil;

public class ByteCodec implements Codec<Byte> {
    @Override
    public Byte decode(byte[] data) {
        final int length = ArrayUtil.arrayLength(data);
        if (length != 2) {
            throw new IllegalArgumentException("byte数组长度必须为2");
        }
        return data[1];
    }

    @Override
    public byte[] encode(Byte data) {
        return new byte[]{TypeEnum.BYTE.getFlags(), data};
    }

    @Override
    public TypeEnum[] supportedTypes() {
        return new TypeEnum[]{TypeEnum.BYTE};
    }
}
